//
//  AppDelegate.m
//  project
//
//  Created by hu on 2019/3/20.
//  Copyright © 2019 hu. All rights reserved.
//

#import "AppDelegate.h"
#import "HJLoginVC.h"
#import "HJTabBarViewController.h"
#import "AppDelegate+HJRCIMDeleagte.h"
#import "HJLocationManager.h"
#import "YFPayManager.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <RongIMKit/RongIMKit.h>
#import "HJChatRoomVC.h"
#import "BaiduMobStat.h"
#import "YFMobManager.h"
#import <WXApi.h>
#import "BDGuideInterfaceView.h"
#import "OpenInstallSDK.h"

@interface AppDelegate () <WXApiDelegate>//UITabBarControllerDelegate

@end

@implementation AppDelegate 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self rootVC];
    [self configureUMessageWithAppKey:RCIMAppKey launchOptions:launchOptions application:application];
    [AMapServices sharedServices].apiKey = AmapAppKey;
    
    // 设置百度统计初始化
    [[BaiduMobStat defaultStat] startWithAppId:BaiduMobAppKey];
    // 三方登录
    [YFMobManager registerAllPlatforms];
    [self guideInterfaceSet];
    
    // OpenInstallSDK
    [OpenInstallSDK initWithDelegate:self];
    //用户注册成功后调用
    [OpenInstallSDK reportRegister];
    
    //键盘设置
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    [manager setEnable:YES];
    manager.shouldResignOnTouchOutside = YES;
    manager.enableAutoToolbar = NO;
    manager.keyboardDistanceFromTextField = 1;
    [self listenNetWorkingStatus];
    
    return YES;
    
}

-(void)rootVC {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    BOOL isLogon = [[mUserDefaults objectForKey:@"isLogin"] isEqualToString:@"1"];
    if (!isLogon) {
        HJLoginVC *vc = [[HJLoginVC alloc]init];;
        vc.isAppDelegate = YES;
        self.window.rootViewController = vc;
    }else {
        HJTabBarViewController * tabbar = [[HJTabBarViewController alloc] init];
//        tabbar.delegate = self;
        self.window.rootViewController = tabbar;
        
    }
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
}

// 引导页
- (void)guideInterfaceSet {
    BOOL idGuideShow = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isGuideShow"] boolValue];
    if (idGuideShow) return;
    NSArray *images = @[@"",@"",@"",@""];
    BDGuideInterfaceView *guideInterfaceView = [[BDGuideInterfaceView alloc] initWithFrame:[UIScreen mainScreen].bounds imageDataArray:images];
    [self.window addSubview:guideInterfaceView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    //当触摸状态栏的时候发送触摸通知 这样控制器就收到了点击事件
    CGPoint touchLocation = [[[event allTouches] anyObject] locationInView:self.window];
    CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
    if (CGRectContainsPoint(statusBarFrame, touchLocation)) {
        [mNotificationCenter postNotificationName:@"StatusBarTouchBeginNotification" object:nil];
    }
}

-(void)listenNetWorkingStatus {
    //1:创建网络监听者
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager manager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                LKLog(@"未知网络");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                LKLog(@"没有联网");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
                LKLog(@"无线网");
                break;
            default:
                break;
        }
    }];
    //开启网络监听
    [manager startMonitoring];
}

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    //判断是否通过OpenInstall URL Scheme 唤起App
    if  ([OpenInstallSDK handLinkURL:url]) {//必写
        return YES;
    }
    
    return  [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    
    //判断是否通过OpenInstall URL Scheme 唤起App
    if  ([OpenInstallSDK handLinkURL:url]){//必写
        return YES;
    }
    
    BOOL isSuc = [WXApi handleOpenURL:url delegate:self];
    NSLog(@"url %@ isSuc %d",url,isSuc == YES ? 1 : 0);
    return  isSuc;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    [application setApplicationIconBadgeNumber:0]; //清除角标
    [[UIApplication sharedApplication] cancelAllLocalNotifications];//清除APP所有通知消息
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    
}


- (void)applicationWillTerminate:(UIApplication *)application {
    
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    // 设置微信支付回调
    [YFPayManager wechatHandleOpenWith:url];
    
    // 设置支付宝支付回调
    [YFPayManager aliPayprocessOrderWith:url callback:^(NSDictionary * _Nonnull result) {
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationAliPayResult object:nil userInfo:result];
    }];
    
    // 设置银联支付回调
    [YFPayManager unionPayHandleOpenWith:url callBack:^(NSString * _Nonnull code, NSDictionary * _Nullable result) {
        NSDictionary *userInfo = @{ @"code": code, @"data": result };
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationUnionPayResult object:nil userInfo:userInfo];
    }];
    
    return YES;
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
    //判断是否通过OpenInstall Universal Link 唤起App
    if ([OpenInstallSDK continueUserActivity:userActivity]){//如果使用了Universal link ，此方法必写
        return YES;
    }
    //其他第三方回调；
    return YES;
}
//- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0) {
//    if ([viewController.tabBarItem.title isEqualToString:@"首页"]) {
//        tabBarController.tabBar.backgroundColor = [UIColor blackColor];
//        tabBarController.tabBar.backgroundImage = [UIImage new];
//    }else {
//        tabBarController.tabBar.backgroundColor = [UIColor whiteColor];
//    }
//    return YES;
//}

@end
